<template>
  <div class="rank-page">
    <el-table :data="tableData" v-loading="loading" stripe style="width: 100%">
      <el-table-column type="index" width="50" />
      <el-table-column prop="player" label="Owner" show-overflow-tooltip width="200px">
        <template #default="scope">
          <el-link type="primary" :href="`https://rinkeby.etherscan.io/address/${scope.row.player}`" target="_blank">
            {{ scope.row.player }}
          </el-link>
        </template>
      </el-table-column>
      <el-table-column prop="tokenId" label="TokenId" align="center">
        <template #default="scope">
          <el-link type="primary"
            :href="`https://rinkeby.etherscan.io/token/${gameContract}?a=${scope.row.tokenId}#inventory`"
            target="_blank">
            {{ scope.row.tokenId }}
          </el-link>
        </template>
      </el-table-column>
      <el-table-column prop="score" sortable label="Score" />
      <el-table-column prop="step" sortable label="Step" />
      <el-table-column prop="duration" sortable label="duration" />
      <el-table-column prop="startTime" label="startTime" />
      <el-table-column prop="endTime" label="endTime" />
    </el-table>
  </div>
</template>

<script setup>
import { graphql } from '@/api'
import { watchEffect, ref, watch } from 'vue'
import dayjs from 'dayjs'
import { useEthersStore } from '@/stores/modules/providers'
import { useRouter } from 'vue-router'
const router = useRouter()
const userStore = useEthersStore()
const { gameContract, account } = userStore
const tableData = ref([])
const loading = ref(false)
const sort = 'tokenId'

if (account) {
  getData(account)
}
watch(userStore, (newVal) => {
  if (newVal.account) {
    getData(newVal.account)
  } else {
    router.push({ name: 'home' })
  }
})
async function getData (address) {
  try {
    const query = `
{
  mintItemEntities(orderBy:${sort},orderDirection:desc, where:{player: "${address}"}) {
    id,
    player,
    tokenId,
    score,
    step,
    startTime,
    endTime,
  }
}
`
    loading.value = true
    const res = await graphql(JSON.stringify({ query }))
    tableData.value = res.mintItemEntities.map(v => {
      v.duration = formatSecond((v.endTime - v.startTime) / 1000)
      v.startTime = dayjs(Number(v.startTime)).format('YYYY-MM-DD HH:mm:ss')
      v.endTime = dayjs(Number(v.endTime)).format('YYYY-MM-DD HH:mm:ss')
      return v
    })
  } catch (error) {
    console.log('[ error ] >', error)
  }
  loading.value = false
}
function formatSecond (second) {
  const hours = Math.floor((second % 86400) / 3600);
  const minutes = Math.floor(((second % 86400) % 3600) / 60);
  const seconds = Math.floor(((second % 86400) % 3600) % 60);
  if (hours > 0) {
    return `${hours}h ${minutes}m ${seconds}s`
  }
  if (minutes > 0) {
    return `${minutes}m ${seconds}s`
  }
  if (seconds > 0) {
    return `${seconds}s`
  }
}


</script>

<style lang="less" scoped>
.rank-page {
  max-width: 1200px;
  margin: 0 auto;
  padding-top: 20px;
  overflow: hidden;
}
</style>